﻿using System;
using System.Web.Mvc;
using PTop.Application.Biz;
using PTop.Code;
using PTop.Code.Excel;
using PTop.Domain.Entity.Biz;

namespace PTop.Web.Areas.Biz.Controllers
{
    /// <summary>
    /// 意见Controller
    /// </summary>	
    public class MessageController : ControllerBase
    {
        private MessageApp _app = new MessageApp();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            var data = _app.GetList(pagination, keyword);
            return PageContent(pagination.records, pagination.rows, data);
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = _app.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(MessageEntity entity, string keyValue)
        {
            _app.SubmitForm(entity, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            _app.Delete(keyValue);
            return Success("删除成功。");
        }

        /// <summary>
        /// 下载统计结果
        /// </summary>
        /// <returns></returns>
        [HandlerAuthorize]
        public ActionResult Download()
        {
            var pagination = new Pagination
            {
                page = 1,
                rows = 100000,
                sidx = "F_CreatorTime desc",
                sord = "asc"
            };
            var list = _app.GetList(pagination, null);
            foreach (var message in list)
            {
                if (!string.IsNullOrEmpty(message.F_Message))
                {
                    message.F_Message = message.F_Message.Replace("\r", "").Replace("\t", "").Replace("\n", "");
                }
            }
            new ExcelExportHelper<MessageEntity>(list)
               .AddProperty(m => m.F_Name, "姓名")
               .AddProperty(m => m.F_Message, "工作")
               .AddProperty(m => m.F_Phone, "电话")
               .AddProperty(m => m.F_Response, "回复")
               .AddProperty(m => m.F_ResponseTime, "回复时间")
               .AddProperty(m => m.F_State, "处理状态", MessageEntity.GetStateName)
               .AddProperty(m => m.F_CreatorTime, "时间")
               .ToExcel("意见信息导出-" + DateTime.Now.ToString("yyyyMMddhh"));
            return new EmptyResult();
        }

    }
}